package com.cskaoyan.gateway.controller.user;


import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("user")
public class LoginController {
    @Reference(timeout = 3000 , check = false)
    ILoginService iLoginService;


    @Reference(timeout = 3000 , check = false)
    IKaptchaService iKaptchaService;


    @Anoymous
    @PostMapping("login")
    public ResponseData login(@RequestBody Map map , HttpServletRequest request , HttpServletResponse response){
        String userName = (String) map.get("userName");
        String userPwd = (String) map.get("userPwd");
        String captcha = (String) map.get("captcha");
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setPassword(userPwd);
        //验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String captchaUuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setCode(captcha);
        kaptchaCodeRequest.setUuid(captchaUuid);
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);

        if (kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.KAPTCHA_CODE_ERROR.getCode())){
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        UserLoginResponse userLoginResponse = iLoginService.Login(userLoginRequest);
        if (userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            Cookie cookie = CookieUtil.genCookie("access_token", userLoginResponse.getToken(), "/" ,  3600);
            cookie.setHttpOnly(true);
            CookieUtil.setCookie(response , cookie);
            return new ResponseUtil<>().setData(userLoginResponse);
        }

        return new ResponseUtil<>().setErrorMsg(userLoginResponse.getMsg());
    }

    @GetMapping("login")
    public ResponseData login(HttpServletRequest request){

        CheckAuthRequest checkAuthRequest = new CheckAuthRequest();

        String token = CookieUtil.getCookieValue(request, "access_token");
        checkAuthRequest.setToken(token);
        CheckAuthResponse checkAuthResponse = iLoginService.validToken(checkAuthRequest);

        String uuid = CookieUtil.getCookieValue(request, "captcha_uuid");
        UserVerifyResponse userVerifyResponse = iLoginService.selectVerify(uuid);
        if (userVerifyResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(checkAuthResponse);
        }
        return new ResponseUtil<>().setErrorMsg(checkAuthResponse.getMsg());
    }

    @GetMapping("loginOut")
    public ResponseData loginOut(HttpServletRequest request , HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
//        HttpSession session = request.getSession();
//
//        String token = CookieUtil.getCookieValue(request, "access_token");
//        session.removeAttribute(token);
//        session.invalidate();
        if (ArrayUtils.isNotEmpty(cookies)){
            for (Cookie cookie : cookies) {
                if (StringUtils.equals(cookie.getName() , "access_token")){
                    cookie.setMaxAge(0);
                    cookie.setPath("/");
//                cookie.setHttpOnly(false);
                    CookieUtil.setCookie(response , cookie);
                    return new ResponseUtil<>().setData(null);
                }
            }
        }
        return new ResponseUtil<>().setErrorMsg("用户没有登陆,请登录");
    }
}
